package com.ligongmusic.service;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.SystemClock;

public class MusicService extends Service{
	private MediaPlayer mPlayer;
    public static final int START_MUSIC = 0;
    public static final int PAUSE_MUSIC = 1;
    public static final int CONTINUE_MUSIC = 2;
    public static final int STOP_MUSIC = 3;
    public static final int SEEK_TO_PROGRESS = 4;
    public static final int DURATION_TYPE = 0;
    public static final int CURRENT_TIME_TYPE = 1;
    private Messenger mMessenger;
	
	public int onStartCommand(Intent intent, int flags, int startId) {
		int status = intent.getIntExtra("status", -1);
		switch (status) {
		case START_MUSIC:
			// 播放音乐
			playNewMusic(intent);
			break;
		case PAUSE_MUSIC:
			// 暂停
			pauseMusic(intent);
			break;
		case CONTINUE_MUSIC:
			// 继续
			restartMusic(intent);
			break;
		case STOP_MUSIC:
			// 停止
			stopMusic(intent);
			break;
		case SEEK_TO_PROGRESS:
			// 移动进度条
			playMusicByProgress(intent);
			break;
		default:
			// 第一次创建时，获取到Activity中的Messenger对象
			mMessenger = (Messenger) intent.getExtras().get("messenger");
			break;
		}
        return super.onStartCommand(intent, flags, startId);
    }
	 
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
	private void playNewMusic(Intent intent){
		String musicPath = intent.getStringExtra("musicName");
		if(mPlayer == null)
			mPlayer = new MediaPlayer();
		mPlayer.reset();
		try{
			mPlayer.setDataSource(musicPath);
			mPlayer.prepare();
			mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {
					mPlayer.start();
					// 进度条时间
					MusicThread thread = new MusicThread();
					thread.start();
				}
			});
		}catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    private void pauseMusic(Intent intent){
    	if(mPlayer == null)
    		playNewMusic(intent);
    	else{
    		mPlayer.pause();
    		// 进度条时间
			MusicThread thread = new MusicThread();
			thread.start();
    	}
    }
    
    private void stopMusic(Intent intent){
    	mPlayer.stop();
    	mPlayer.release();
    	mPlayer = null;
    }
    
    private void restartMusic(Intent intent){
    	if(mPlayer == null)
    		playNewMusic(intent);
    	else{
    		mPlayer.start();
    		// 进度条时间
			MusicThread thread = new MusicThread();
			thread.start();
    	}
    }
    
    private void playMusicByProgress(Intent intent){
    	
    }
    
    class MusicThread extends Thread{
    	@Override
    	public void run() {
    		while(mPlayer.isPlaying()){
    			try {
    				Message msg = new Message();
    				msg.what = CURRENT_TIME_TYPE;
    				msg.obj = mPlayer.getDuration();// 发送当前时长
					mMessenger.send(msg);
					SystemClock.sleep(1000);// 一秒发送一次Handler
				} catch (RemoteException e) {
					e.printStackTrace();
				}
    		}
    	}
    }
    
}
